﻿using Butcher.winform.Dto;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;

namespace Butcher.winform.EnterpriseForms
{
    public partial class Scales : Form
    {
        public Scales()
        {
            InitializeComponent();
            getType();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Scale res = new Scale();
            string url = @"http://localhost:5229/api/Section/ScaleSettingCreate";
            HttpClient client = new HttpClient();
            res.ScaleName = ScaleName.Text;
            res.ScaleTypeId = Convert.ToInt32(cb_Level.SelectedValue);
            if (UseType1.Checked)
            {
                res.UseType = 0;
            }
            if (UseType2.Checked)
            {
                res.UseType = 1;
            }
            res.TareWeightDeduction = Convert.ToDecimal(textBox3.Text);
            if (radioButton4.Checked)
            {
                res.NumberOfPieces = 0;
            }
            if (radioButton3.Checked)
            {
                res.NumberOfPieces = 1;
            }
            res.Warehouse = Convert.ToInt32(textBox5.Text);
            if (radioButton6.Checked)
            {
                res.InAndOutOfStorage = "0";
            }
            if (radioButton5.Checked)
            {
                res.InAndOutOfStorage = "1";
            }
            res.Operator = textBox4.Text;
            res.CreatedBy = "李斯";
            res.ModifiedBy = "李斯";
            res.CreateTime = DateTime.Now;
            var content = new StringContent(JsonSerializer.Serialize<Scale>(res));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            if (info != null)
            {
                MessageBox.Show("新增成功");
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }

        public void getType()
        {
            ScaleType res = new ScaleType();
            string url = @"http://localhost:5229/api/Section/ScaleTypeList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<ScaleType>(res));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<ScaleType> enterprises = aaa.ToObject<List<ScaleType>>();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "ScaleTypename";
            this.cb_Level.ValueMember = "ScaleTypeid";
        }
    }
}
